package jsyf.oa.service.device;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.device.DeviceDao;
import jsyf.oa.entity.device.*;
import jsyf.oa.facade.device.DeviceFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LT on 2018/3/6.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class DeviceService implements DeviceFacade {

    @Autowired
    private DeviceDao deviceDao;

    @Override
    public JSONObject getDeviceList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        //公司名称
        String companyName = jsonParams.getString("companyName");
        //品牌
        String brandName = jsonParams.getString("brandName");
        //型号
        String modelName = jsonParams.getString("modelName");
        //斗容
        String bucketCapacity = jsonParams.getString("bucketCapacity");
        //序列号
        String sequence = jsonParams.getString("sequence");
        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //传参
        StringBuffer condition = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();
        if (!StringUtils.isBlank(companyName)) {
            condition.append(" AND companyName=?");
            sqlArgs.add(companyName);
        }
        if (!StringUtils.isBlank(brandName)) {
            condition.append(" AND brandName=?");
            sqlArgs.add(brandName);
        }
        if (!StringUtils.isBlank(modelName)) {
            condition.append(" AND modelName=?");
            sqlArgs.add(modelName);
        }
        if (!StringUtils.isBlank(bucketCapacity)) {
            condition.append(" AND bucketCapacity=?");
            sqlArgs.add(bucketCapacity);
        }
        if (!StringUtils.isBlank(sequence)) {
            condition.append(" AND sequence like ?");
            sqlArgs.add("%" + sequence + "%");
        }
        condition.append("AND delFlag = 0");
        condition.append(" ORDER BY createTime DESC");
        //查询list
        List<JSONObject> records = deviceDao.getViewJSONListByPage(condition.toString(), sqlArgs.toArray(), requestPage, pageSize);
        //查询总数
        Integer recordCount = deviceDao.getViewPageCount(condition.toString(), sqlArgs.toArray());

        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    @Override
    public List<JSONObject> getDeviceByCompanyId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        //公司名称
        String companyId = jsonParams.getString("companyId");
        return deviceDao.getDeviceByCompanyId(" and t_sys_company.ID=?" ,new Object[]{companyId});

    }

    @Override
    public JSONObject add(String jsonData) {
        DeviceEntity deviceBrandEntity = JSON.toJavaObject(JSON.parseObject(jsonData), DeviceEntity.class);
        JSONObject result = new JSONObject();
        result.put("addNewId", deviceDao.add(deviceBrandEntity));
        return result;
    }

    @Override
    public JSONObject getById(String jsonData) {
        return deviceDao.getJSONById(JSON.parseObject(jsonData).getString("id"));
    }

    @Override
    public Boolean update(String jsonData) {
        DeviceBrandEntity deviceBrandEntity = JSON.toJavaObject(JSON.parseObject(jsonData), DeviceBrandEntity.class);
        return deviceDao.update(deviceBrandEntity) > 0;
    }

    @Override
    public Boolean delById(String jsonData) {
        return deviceDao.delById(JSON.parseObject(jsonData).getString("id")) > 0;
    }

    @Override
    public Boolean removeById(String jsonData) {
        return deviceDao.removeById(JSON.parseObject(jsonData).getString("id")) > 0;
    }

    @Override
    public Boolean updateDevice(String jsonData) {
        DeviceEntity deviceEntity = JSON.toJavaObject(JSON.parseObject(jsonData), DeviceEntity.class);
        Integer rows = 0;
        rows = deviceDao.update(deviceEntity);
        return rows > 0;
    }

    @Override
    public Boolean saveDevice(String jsonData) {
        DeviceEntity deviceEntity = JSON.toJavaObject(JSON.parseObject(jsonData), DeviceEntity.class);

        Boolean bool = (deviceDao.add(deviceEntity) > 0);
        return bool;

    }

    @Override
    public Boolean saveOldCustomer(String jsonData) {
        CustomerEntity customerEntiy= JSON.toJavaObject(JSON.parseObject(jsonData), CustomerEntity.class);
        // JSONObject result = new JSONObject();
        Boolean bool = (deviceDao.add(customerEntiy) > 0);
        return bool;
    }

    @Override
    public Boolean saveAssess(String jsonData) {
        AssessEntity assessEntity= JSON.toJavaObject(JSON.parseObject(jsonData), AssessEntity.class);
        // JSONObject result = new JSONObject();
        Boolean bool = (deviceDao.add(assessEntity) > 0);
        return bool;
    }

    @Override
    public Boolean saveSalesCost(String jsonData) {
        SalesCostEntity salesCostEntiy= JSON.toJavaObject(JSON.parseObject(jsonData), SalesCostEntity.class);
        // JSONObject result = new JSONObject();
        Boolean bool = (deviceDao.add(salesCostEntiy) > 0);
        return bool;
    }

    @Override
    public JSONObject getDevicePage(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String brandName = jsonParams.getString("brandName");
        String deviceTypeName = jsonParams.getString("deviceTypeName");
        String key = jsonParams.getString("key");
        int requestPage = 1;
        int pageSize = 20;
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //传参
        StringBuffer condition = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();
        if (!StringUtils.isBlank(deviceTypeName)) {
            if(!"设备类型".equals(deviceTypeName)) {
                condition.append(" AND t_dvc_device_type.`NAME`=?");
                sqlArgs.add(deviceTypeName);
            }
        }
        if (!StringUtils.isBlank(brandName)) {
            if(!"品牌".equals(brandName)) {
                condition.append(" AND t_dvc_brand.BRAND_NAME=?");
                sqlArgs.add(brandName);
            }
        }
        if(!StringUtils.isBlank(key)){
            condition.append(" AND t_dvc_device_type.`NAME` LIKE '%" + key + "%' or  t_dvc_brand.BRAND_NAME LIKE '%" + key + "%' or t_dvc_model.MODEL_NAME LIKE '%" + key + "%' ");
        }
        condition.append(" AND t_dvc_model.DEL_FLAG = 0 ");
        //查询list
        List<JSONObject> records = deviceDao.getDeviceList(condition.toString(), sqlArgs.toArray(), requestPage, pageSize);
        //查询总数
        Integer recordCount = deviceDao.getPageCount1(condition.toString(), sqlArgs.toArray());
        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    @Override
    public JSONObject getDeviceById(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String id = jsonParams.getString("id");
        return deviceDao.getDeviceById(id);
    }
}
